#!/usr/bin/env python3
# coding=utf-8
import os
import sys
import tempfile
import time

import yaml
import subprocess
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtGui import QIcon

from MetLifeAutomation import Ui_MainWindow


class Main(QtWidgets.QMainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        self.setWindowIcon(QIcon("main.ico"))
        # 给button 的 点击动作绑定一个事件处理函数
        config_dict = self.read_yaml_config()
        self.set_include_tags_select(config_dict['tags'])
        self.ui.RunScriptButton.clicked.connect(self.exec_command)


    def exec_command(self):
        print(self.ui.exclude_lineEdit.text())
        print("====================>")
        p = subprocess.Popen(r"robot  t1.robot", shell=True, stdout=subprocess.PIPE)

        while True:
            s=p.stdout.readline().decode()
            print(s)
            self.ui.plainTextEdit.appendPlainText(s)
            if s == "":
                break

    def read_yaml_config(self):
        config_dict = dict()
        with open("GSSP_Tags.yaml") as f:
            config_dict = yaml.load(f, Loader=yaml.FullLoader)
        return config_dict

    def set_include_tags_select(self, tags):
        for i in tags:
            self.ui.include.addItem(i, i)
            self.ui.exclude.addItem(i, i)


if __name__ == '__main__':
    app = QtWidgets.QApplication(sys.argv)
    window = Main()
    window.show()
    sys.exit(app.exec_())
